package com.gjy.mybatis.plugin;

import org.apache.ibatis.executor.parameter.ParameterHandler;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.plugin.Intercepts;
import org.apache.ibatis.plugin.Invocation;
import org.apache.ibatis.plugin.Signature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.sql.PreparedStatement;

/**
 * @author 宫静雨
 * @version 1.0
 * @since 2023-10-24 14:15:35
 */
@Intercepts({
        @Signature(type = ParameterHandler.class, method = "setParameters", args = {PreparedStatement.class}),
        @Signature(type = ParameterHandler.class, method = "getParameterObject", args = {}),
})
public class ParameterHandlerInterceptor implements Interceptor {
    private static final Logger log = LoggerFactory.getLogger(ParameterHandlerInterceptor.class);

    @Override
    public Object intercept(Invocation invocation) throws Throwable {
        String name = invocation.getMethod().getName();
        Object proceed = invocation.proceed(); // 参数
        if ("setParameters".equals(name)) {
            PreparedStatement ps = (PreparedStatement) invocation.getArgs()[0];
            ps.setString(1, "M");
            log.info("设置参数");
        } else if ("getParameterObject".equals(name)) {
            log.info("参数: {}", proceed);
        } else {
            log.error("method name: {} nonentity", name);
        }
        return proceed;
    }

}
